package com.yixincaipiao.ui.account;

import android.content.Context;
import android.content.SharedPreferences;

import com.yixincaipiao.common.CommonConfig;
import com.yixincaipiao.ui.account.modle.User;
import com.yixincaipiao.utils.JsonUtil;

/**
 * 单例模式  管理用户信息    //增删改查
 */

public class Account {

    private static Account me;
    private Context mContext;
    public boolean isRegetData = false;
    public User mUser;

    private Account(Context mContext) {
        super();
        this.mContext = mContext;
        initUserData();
    }

    private User mUserBean;

    public static Account Instance(Context mContext) {

        if (me == null) {
            synchronized (Account.class) {
                if (me == null) {
                    me = new Account(mContext);
                }
            }
        }
        return me;
    }

    public void initUserData() {
        String loginUserInfo = CommonConfig.instance().sharedPreferences.getString("userinfo", "");
        if (loginUserInfo != null) {
            mUser = JsonUtil.parseJson(loginUserInfo, User.class);
        }

    }

    public User getUser() {
        return mUser;
    }

    public void saveUser(String userInfo) {
        if (userInfo != null) {
            mUser = JsonUtil.parseJson(userInfo, User.class);
            SharedPreferences.Editor editor = CommonConfig.instance().editor;
            editor.putString("userinfo", userInfo);
            editor.commit();
        }

    }

    /**
     * 清空用户信息
     */
    public void logout() {
        SharedPreferences.Editor editor = CommonConfig.instance().editor;
        editor.putString("userinfo", "");
        editor.commit();
        mUser = null;

    }


}
